import {default as api} from '../../utils/api'
import common from "../../utils/common.vue";

const user = {
    state: {
        username: "",
        userId: "",
        address: "",
    },
    mutations: {
        SET_USER: (state, userInfo) => {
            state.username = userInfo.username;
            state.userId = userInfo.userId;
            state.address = userInfo.address;
        },
        RESET_USER: (state) => {
            state.username = "";
            state.userId = "";
            state.address = "";
        },
    },
    actions: {
        Login({commit, state}, loginForm) {
            return new Promise((resolve, reject) => {
                api({
                    url: "user/login",
                    method: "post",
                    data: loginForm
                }).then(data => {
                    if (common.codeFromBackEnd.SUCCESS_CODE === data.code){
                        commit('SET_USER', data.info);
                    }
                    resolve(data);
                }).catch(err => {
                    console.log("进入错误信息啦~")
                    reject(err)
                })
            })
        },  
        Logout({commit}) {
            return new Promise((resolve) => {
                // api({
                //     url: "user/logout",
                //     method: "post"
                // }).then(data => {
                    commit('RESET_USER')
                //     resolve(data);
                // }).catch(() => {
                //     commit('RESET_USER')
                // })
            })
        },
    }
}
export default user
